package com.zuxia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuxia.dto.ShopDto;
import com.zuxia.dto.ShopReleaseDto;
import com.zuxia.entity.Group;
import com.zuxia.entity.PageDto;
import com.zuxia.entity.Type;
import com.zuxia.entity.Shop;
import com.zuxia.exception.ZuxiaBusinessException;
import com.zuxia.mapper.ShopMapper;
import com.zuxia.param.ShopPageParam;
import com.zuxia.param.ShopReleasePageParam;
import com.zuxia.service.ShopService;
import com.zuxia.sql.ShopSql;
import com.zuxia.utils.ResultCodeEnum;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoli
 * @since 2024-11-14
 */
@Service
@AllArgsConstructor
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    private static String sql;

    @Resource
    private ShopMapper shopMapper;

    @Override
    public PageDto<ShopDto> getShopListPage(ShopPageParam shopPageParam) throws Exception {
        sql=ShopSql.ShopLeftJoinType;
        if(!shopPageParam.getId().isEmpty()){
            sql+="and shop_id like '%"+shopPageParam.getId()+"%' or shop_name like '%"+shopPageParam.getId()+"%' ";
        }
        if(shopPageParam.getShopStatus()!=null){
            sql+="and shop_status="+shopPageParam.getShopStatus()+" ";
        }
        if(shopPageParam.getShopType()!=null){
            sql+="and shop_type="+shopPageParam.getShopType()+" ";
        }
        IPage<ShopDto> shopListPage = shopMapper.getShopListPage(new Page<>(shopPageParam.getPage(), shopPageParam.getSize()), sql);
        List<ShopDto> records = shopListPage.getRecords();
        PageDto<ShopDto> shopDtoPageDto = new PageDto<>();
        shopDtoPageDto.setList(records);
        shopDtoPageDto.setTotal(shopListPage.getTotal());
        return shopDtoPageDto;
    }

    @Override
    public ShopDto getShopById(String id) throws Exception{
        sql=ShopSql.ShopLeftJoinType;
        sql+="and shop_id='"+id+"' ";
        ShopDto shopDto = shopMapper.getShopById(sql);
        if(shopDto==null){
            throw new ZuxiaBusinessException(ResultCodeEnum.SHOP_NULL);
        }
        return shopDto;
    }

    @Override
    public boolean upShopNumById(String id, Long num) {
        Shop shop = new Shop();
        shop.setShopId(id);
        shop.setShopNum(num);
        int i = shopMapper.updateById(shop);
        if(i==1){
            return true;
        }
        throw new ZuxiaBusinessException(ResultCodeEnum.SHOP_NUMBER_IS_UP);
    }

    @Override
    public PageDto<ShopReleaseDto> getShopReleaseListPage(ShopReleasePageParam shopReleasePageParam) throws Exception {
        sql=ShopSql.GroupShopType;
        if(!shopReleasePageParam.getId().isEmpty()){
            sql+=" and c.shop_id like '%"+shopReleasePageParam.getId()+"%' "+"and c.shop_name like '%"+shopReleasePageParam.getId()+"%' ";
        }
        if(shopReleasePageParam.getShopType()!=null&&shopReleasePageParam.getShopType()!=0){
            sql+="and a.shop_type="+shopReleasePageParam.getShopType()+" ";
        }
        sql+=" and group_release_status=1 ";
        IPage<ShopReleaseDto> shopReleaseListPage = shopMapper.getShopReleaseListPage(new Page<>(shopReleasePageParam.getPage(), shopReleasePageParam.getSize()), sql);
        List<ShopReleaseDto> records = shopReleaseListPage.getRecords();
        PageDto<ShopReleaseDto> shopReleaseDtoPageDto = new PageDto<>();
        shopReleaseDtoPageDto.setList(records);
        shopReleaseDtoPageDto.setTotal(shopReleaseListPage.getTotal());
        return shopReleaseDtoPageDto;
    }

    @Override
    public ShopReleaseDto getShopReleaseById(String id) throws Exception {
        sql=ShopSql.GroupShopType;
        sql+="and c.group_id='"+id+"' and group_release_status=1";
        ShopReleaseDto shopReleaseById = shopMapper.getShopReleaseById(sql);
        if(shopReleaseById==null){
            throw new ZuxiaBusinessException(ResultCodeEnum.GROUP_IS_RELEASE_NOT);
        }
        return shopReleaseById;
    }
}
